<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="height=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">
    <meta name="keywords" content="天地图"/>
    <title>根据企业具体地址定位</title>
    <script type="text/javascript" src="http://api.tianditu.com/api?v=4.0"></script>
    <style type="text/css">
        body,html{
            width:100%;
            height:100%;
            margin:0;
            font-family:"Microsoft YaHei"
        }
        #mapDiv{
            width:100%;
            height:100%
        }
        .navgation{
            position: absolute;
            right:0;
            top:70%;
            height:50px;
            width:50px;
            font-size:18px;
            color:white;
            padding-top:22px;
            text-align:center;
            background:#2A81CB;
            z-index:1000;
        }
    </style>
    <script>
        var map;
        var zoom = 12;
        var localsearch;
        var drivingRoute;    //驾车路线规划对象
        var startLngLat;  //起点经纬度
        var endLngLat;      //终点经纬度
        var infoWin = null;
        var lo;
        function onLoad() {
            //初始化地图对象
            map = new T.Map("mapDiv");
            //设置显示地图的中心点和级别
            map.centerAndZoom(new T.LngLat(116.40969, 39.89945), zoom);

            var config = {
                pageCapacity: 10,   //每页显示的数量
                onSearchComplete: localSearchResult //接收数据的回调函数
            };
            //创建搜索对象
            localsearch = new T.LocalSearch(map, config);
            getPosition();
            var config = {
                policy: 3,  //驾车策略
                onSearchComplete: searchResult  //检索完成后的回调函数
            };
            drivingRoute = new T.DrivingRoute(map, config);
            lo  = new T.Geolocation();
        }

        function localSearchResult(result) {
            console.log(result)
            //获取查询结果集(理论上只能有一个点数据)
            if(parseInt(result.getResultType())==1){
                pois(result.getPois());
            }else{
                alert("未检索到相关地理位置信息")
            }
        }

        //解析点数据结果
        function pois(obj) {
            if (obj) {
                //坐标数组，设置最佳比例尺时会用到
                var zoomArr = [];
                for (var i = 0; i < obj.length; i++) {
                    //闭包
                    (function (i) {
                        //名称
                        var name = obj[i].name;
                        //地址
                        var address = obj[i].address;
                        //坐标
                        var lnglatArr = obj[i].lonlat.split(" ");
                        var lnglat = new T.LngLat(lnglatArr[0], lnglatArr[1]);
                        endLngLat = lnglat;
                        var winHtml = "名称:" + name + "<br/>地址:" + address;

                        //创建标注对象
                        var marker = new T.Marker(lnglat);
                        //地图上添加标注点
                        map.addOverLay(marker);
                        //注册标注点的点击事件
                        var markerInfoWin = new T.InfoWindow(winHtml, {autoPan: true});
                        marker.addEventListener("click", function () {
                            marker.openInfoWindow(markerInfoWin);
                        });

                        zoomArr.push(lnglat);
                        showPosition(marker, winHtml);
                    })(i);
                }
                //显示地图的最佳级别
                map.setViewport(zoomArr);
            }
        }

        //显示信息框
        function showPosition(marker, winHtml) {
            var markerInfoWin = new T.InfoWindow(winHtml, {autoPan: true});
            marker.openInfoWindow(markerInfoWin);
        }

        //获取用户传过来的企业位置信息
        function getPosition(){
            if(GetQueryString("address")){
                console.log(decodeURIComponent(GetQueryString("address")));
                localsearch.search(GetQueryString("address"));
            }else{
                alert("地址信息不正确,无法定位");
                window.location.href=encodeURI(encodeURI("http://192.168.50.212/getLocationByName.html?address=北京市海淀区上地信息路1号金远见大楼"));
                // history.go(-1);
            }
        }

        //根据key获取请求字符串中的value
        function GetQueryString(name)
        {
            var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
            var r = window.location.search.substr(1).match(reg);
            if(r!=null)return  unescape(r[2]); return null;
        }

        //驾车导航
        function navgation(){
            getCurrentPosition();
             //驾车路线搜索
            // drivingRoute.search(startLngLat, endLngLat);
        }

         function searchResult(result) {
            //获取方案个数
            var routes = result.getNumPlans();
            for (var i = 0; i < routes; i++) {

                //获得单条驾车方案结果对象
                var plan = result.getPlan(i);
                //显示驾车线路
                createRoute(plan.getPath());
                //显示最佳级别
                map.setViewport(plan.getPath());
            }
        }

        function createRoute(lnglats) {
            //创建线对象
            var line = new T.Polyline(lnglats, {color: "#2C64A7", weight: 5, opacity: 0.9});
            //向地图上添加线
            map.addOverLay(line);
        }

        //获取用户当前的坐标
        function getCurrentPosition(){
            // startLngLat = new T.LngLat(116.32324,39.86785);
            lo.getCurrentPosition(function(e){
                 if (this.getStatus() == 0){
                    map.centerAndZoom(e.lnglat, 15)
                    alert("获取定位坐标："+e.lnglat.lat + "," + e.lnglat.lng)
                    var marker = new T.Marker(e.lnglat);
                    map.addOverLay(marker);
                }
                if(this.getStatus() == 1){
                    map.centerAndZoom(e.lnglat, e.level)
                    alert("获取定位坐标："+e.lnglat.lat + "," + e.lnglat.lng)
                    var marker = new T.Marker(e.lnglat);
                    map.addOverLay(marker);
                }
                startLngLat = e.lnglat;
            });
        }

    </script>
</head>
<body onLoad="onLoad()">
<div class="navgation" onclick="navgation()">路线</div>
<div id="mapDiv"></div>
<!-- <div>
    <input type="text" id="keyWord" value="北京市海淀区上地信息路1号金远见大楼"/>
    <input type="button" onClick="localsearch.search(document.getElementById('keyWord').value)" value="搜索"/>
    <br/>
</div> -->
</body>
</html>

